package CustomCMS
{
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextLineMetrics;
	
	import mx.events.FlexEvent;
	import mx.core.ScrollPolicy;

	public class ExpandingTextEditor extends TextEditor
	{		
		public function ExpandingTextEditor()
		{
			super();
			this.verticalScrollPolicy = ScrollPolicy.OFF;
			this.addEventListener(Event.CHANGE, AutoResize);
			this.addEventListener(FlexEvent.VALUE_COMMIT, AutoResize);
			callLater(AutoResize, [null]);
		}
		
		override protected function createChildren() : void
	    {
	        super.createChildren();
	        ModifyTextField();
	    }
	
	    override protected function commitProperties() : void
	    {
	    	super.commitProperties();
	        ModifyTextField();
	    }
	    
	    private function ModifyTextField() : void
	    {
	    	if (!textField) return;
			textField.autoSize = TextFieldAutoSize.LEFT;
//			textField.addEventListener(Event.CHANGE, AutoResize);
	    }
		
		protected function AutoResize(event:Event) : void
		{
			if (!textField) return;
			textField.validateNow();
			var t:TextLineMetrics = this.measureText("\n");
			this.height = textField.textHeight + t.height - t.leading;
			this.verticalScrollPosition = 0;
		}
	}
}